package org.arga.empresa.model.useradmin;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Model object to describe a user and it's credentials of the Open Empresa Suite. 
 * <p>In most cases, credentials are associated with business rules that describe
 * different use cases. A good example is the case of "expired accounts" vs
 * "expired credentials". One may think of the account as the access to the system,
 * whereas a credentials to refer to the policy behind authenticating this user.
 *
 * @author aflores@arga-inc.com
 * @since Jan 23, 2010 09:46:27 PM
 */
@Entity
@Table(name="users")
@NamedQueries({
  // select user by username
  @NamedQuery(name="user.findByUsername",
		      query="SELECT u FROM User u WHERE u.username = :username")
})
public class User {
	private Long m_id;
	private String m_username;
	private String m_password;
	private String m_email;
	private boolean m_enabled;
	private boolean m_locked;
	private boolean m_expiredAccount;
	private boolean m_expiredCredentials;
	private List<Group> m_groups;
	
	/**
	 * {@inheritDoc} 
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long getId() {
		return m_id;
	}

	/**
	 * {@inheritDoc} 
	 */
	public void setId(Long id) {
		m_id = id;
	}

	/**
	 * Retrieves the username of this user entity.
	 * @return the username. 
	 */
	@Column(length=50, unique=true, nullable=false)
	public String getUsername() {
		return m_username;
	}

	/**
	 * Sets the username of this entity.
	 * @param username the username to set. 
	 */
	public void setUsername(String username) {
		m_username = username;
	}

	/**
	 * Retrieves the password of this user.
	 * @return the password.
	 */
	@Column(length=50, nullable=false)
	public String getPassword() {
		return m_password;
	}

	/**
	 * Sets the password of this user.
	 * @param password the password to set.
	 */
	public void setPassword(String password) {
		m_password = password;
	}
	
	/**
	 * Retrieves the email address of this user.
	 * @return the email address.
	 */
	@Column(length=100)
	public String getEmail() {
		return m_email;
	}

	/**
	 * Sets the email of this user.
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		m_email = email;
	}

	/**
	 * Retrieves a flag indicating whether this user is enabled or not.
	 * @return true if this user is enabled, false otherwise.
	 */
	public boolean isEnabled() {
		return m_enabled;
	}

	/**
	 * Sets a flag to indicate whether this user is enabled or not.
	 * @param enabled the flag to set.
	 */
	public void setEnabled(boolean enabled) {
		m_enabled = enabled;
	}

	/**
	 * Retrieves a flag indicating whether this user account is locked.
	 * @return true if this user's account is locked, false otherwise.
	 */
	public boolean isLocked() {
		return m_locked;
	}

	/**
	 * Sets a flag to indicate whether this user's account is locked.
	 * @param locked the flag to set.
	 */
	public void setLocked(boolean locked) {
		m_locked = locked;
	}

	/**
	 * Retrieves a flag indicating whether this user's account is expired or not.
	 * @return true if this account is expired, false otherwise. 
	 */
	public boolean isExpiredAccount() {
		return m_expiredAccount;
	}

	/**
	 * Sets the flag indicating whethe this account is expired.
	 * @param expiredAccount the flag to set.
	 */
	public void setExpiredAccount(boolean expiredAccount) {
		m_expiredAccount = expiredAccount;
	}

	/**
	 * Checks whether the current user's credentials are <b>EXPIRED</b>.
	 * @return true if this credentials are expired, false otherwise. 
	 */
	public boolean isExpiredCredentials() {
		return m_expiredCredentials;
	}

	/**
	 * Sets the credentials as <b>EXPIRED</b>.
	 * @param expiredCredentials the flag to set.
	 */
	public void setExpiredCredentials(boolean expiredCredentials) {
		m_expiredCredentials = expiredCredentials;
	}

	/**
	 * Retrives a collection of all groups this users belongs to.
	 * @return the groups this user belongs to.
	 */
	@ManyToMany
	@JoinTable(name="users_groups")
	public List<Group> getGroups() {
		return m_groups;
	}

	/**
	 * Sets all groups this user belongs to.
	 * @param groups the groups this user belongs to. 
	 */
	public void setGroups(List<Group> groups) {
		m_groups = groups;
	}
}
